package com.pdsu.xpf.graduation01.control;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.xpf.graduation01.dto.PayCommodityDTO;
import com.pdsu.xpf.graduation01.service.impl.PayCommodityServiceImpl;
import com.pdsu.xpf.graduation01.utils.JsonFormat;
import com.pdsu.xpf.graduation01.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/payCommodity")
public class PayCommodityControl {


    @Autowired
    PayCommodityServiceImpl payCommodityService;



    @ResponseBody
    @RequestMapping("/findPayCommodityByUID")
    public Result findPayCommodityByUID(int uid) throws JsonProcessingException {

        List<PayCommodityDTO> payCommodityDTOS=payCommodityService.findPayCommodityByUID(uid);
        String json = JsonFormat.ListToJson(payCommodityDTOS);
        System.out.println("*-----pay"+json);
        return Result.success(json);
    }

    @ResponseBody
    @RequestMapping("/removePayCommodityByUIDAndCID")
    public Result removePayCommodityByUIDAndCID(HttpServletRequest request) throws Exception {

        String json_paycommodity = request.getParameter("list_data");

        int uid = Integer.parseInt(request.getParameter("uid"));

        List<PayCommodityDTO> payCommodityDTOS = JsonFormat.json2list(json_paycommodity,PayCommodityDTO.class);
        for (PayCommodityDTO p:payCommodityDTOS){

            Integer i = payCommodityService.removePayCommodityByUIDAndCID(uid,p.getCid());
        }

        return Result.success();
    }


}
